/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unicen.twitter.suggest.backend.dtos;

import ar.edu.unicen.twitter.suggest.backend.datasources.TwitterFacadeImpl;
import ar.edu.unicen.twitter.suggest.commons.dtos.Usuario;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.RejectedException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.TwitterException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.UserNotFoundException;
import ar.edu.unicen.twitter.suggest.interfaces.exceptions.UserProtectedException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public class UsuarioProxy extends Usuario {

    private TwitterFacadeImpl twitterFacade;
    long id;
    UsuarioApi usuario;

    public UsuarioProxy(long id2, TwitterFacadeImpl twitterFacade) {
        this.twitterFacade = twitterFacade;
        this.id = id2;
        this.usuario = null;

    }

    public UsuarioProxy(String screenName, TwitterFacadeImpl twitterFacade) throws UserNotFoundException, UserProtectedException, RejectedException, TwitterException {
        this.twitterFacade = twitterFacade;
        usuario = twitterFacade.getUserApi(screenName);
        this.id = usuario.getId();


    }

    @Override
    public List<TweetApi> getTweets() {
        checkTweets();
        return usuario.getTweets();
    }

    @Override
    public int getFollowsCount() {
        checkUser();
        return usuario.getFollowsCount();
    }

    @Override
    public int getFollowersCount() {
        checkUser();
        return usuario.getFollowersCount();
    }

    @Override
    public int getTweetsCount() {
        checkUser();
        return usuario.getTweetsCount();
    }

    @Override
    public String getScreenName() {
        checkUser();
        return usuario.getScreenName();
    }

    @Override
    public Collection<Usuario> getFollowers() {
        try {
            return twitterFacade.getUserList(getFollowersIds());
        } catch (UserNotFoundException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UserProtectedException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RejectedException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TwitterException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public long[] getFollowersIds() {
        checkFollowers();
        return usuario.getFollowersIds();
    }

    @Override
    public long[] getFollowsIds() {
        checkFollows();
        return usuario.getFollowsIds();
    }

    @Override
    public Collection<Usuario> getFollows() {
        try {
            return twitterFacade.getUserList(getFollowsIds());
        } catch (UserNotFoundException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UserProtectedException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RejectedException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TwitterException ex) {
            Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public boolean equals(Object o1) {
        if (o1 instanceof Usuario && id == ((Usuario) o1).getId()) {
            return true;
        }
        return false;
    }

    @Override
    public long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        return Usuario.calculateHashCode(id);
    }

    @Override
    public String toString() {
        return "[" + id + "]" + "(" + usuario + "):" + getMentions() + "m, " + getScore() + "sc";
    }

    public String getName() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getLocation() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getDescription() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isContributorsEnabled() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public URL getProfileImageURL() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public URL getURL() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isProtected() {
        checkUser();
            return usuario.isProtected();

    }

    public Date getStatusCreatedAt() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long getStatusId() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getStatusText() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getStatusSource() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isStatusTruncated() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long getStatusInReplyToStatusId() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getStatusInReplyToUserId() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isStatusFavorited() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getStatusInReplyToScreenName() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getProfileBackgroundColor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getProfileTextColor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getProfileLinkColor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getProfileSidebarFillColor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getProfileSidebarBorderColor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getFriendsCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Date getCreatedAt() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getFavouritesCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getUtcOffset() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getTimeZone() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getProfileBackgroundImageUrl() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isProfileBackgroundTiled() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getLang() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getStatusesCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isGeoEnabled() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isVerified() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void instantiate() {
        if (usuario == null) {
            try {
                usuario = twitterFacade.getUserApi(id);
            } catch (UserNotFoundException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UserProtectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RejectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TwitterException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void checkUser() {
        instantiate();
        if (!usuario.getUsuarioBasicoValido()) {
            UsuarioBasico usuarioBasico = null;
            try {
                usuarioBasico = twitterFacade.getUserData(id);
            } catch (UserNotFoundException ex) {
                //Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
                usuarioBasico = new UsuarioBasico();
            } catch (UserProtectedException ex) {
                usuarioBasico = new UsuarioBasico();
                usuarioBasico.setProtected(true);
            } catch (RejectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TwitterException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
            usuario.setUsuarioBasico(usuarioBasico);
            usuario.setUsuarioBasicoValido(true);
            twitterFacade.update(usuario);
        }
    }

    private void checkTweets() {
        instantiate();
        if (!usuario.getTweetsValidos()) {
            try {
                List<TweetApi> twz = twitterFacade.getNewTweets(getScreenName());
                if (twz != null) {
                    usuario.setTw(twz);
                } else {
                    usuario.setTw(new ArrayList<TweetApi>());
                }
                usuario.setTweetsValidos(true);
                twitterFacade.update(usuario);
            } catch (UserNotFoundException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UserProtectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RejectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TwitterException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void checkFollows() {
        instantiate();
        if (!usuario.getFollowsValidos()) {
            try {
                long[] follows = twitterFacade.getFollows(getScreenName(), getFollowsCount());
                usuario.setFws(follows);
                usuario.setFollowsValidos(true);
                twitterFacade.update(usuario);
            } catch (UserNotFoundException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UserProtectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RejectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TwitterException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void checkFollowers() {
        instantiate();
        if (!usuario.getFollowersValidos()) {
            try {
                long[] followers = twitterFacade.getFollowers(getScreenName(), getFollowersCount());
                usuario.setFwers(followers);
                usuario.setFollowersValidos(true);
                twitterFacade.update(usuario);
            } catch (UserNotFoundException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UserProtectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RejectedException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TwitterException ex) {
                Logger.getLogger(UsuarioProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
